package com.csdj.shop.service.user;

import com.csdj.shop.entity.User;
import com.csdj.shop.mapper.UserMapper;
import com.csdj.shop.util.Md5Util;
import com.csdj.shop.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * author:cloud
 * date:2021/2/26 0026
 **/
public class UserService implements IUserService {
    @Override
    public String regUser(String username, String password) {
        SqlSession session = MybatisUtil.getSession(true);
        UserMapper userMapper = session.getMapper(UserMapper.class);
        //判断用户名是否存在
        int userCount = userMapper.findCountByUsername(username);
        if(userCount > 0){
            return "username exists";
        }
        //处理密码
        String dePass = Md5Util.md5(password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(dePass);
        user.setRegTm(new Date());
        int result = userMapper.regUser(user);
        if(result == 1){
            return "注册成功";
        }else{
            return "注册失败";
        }
    }

    @Override
    public String login(HttpServletRequest request, String username, String password) {
        return null;
    }

    @Override
    public int updateUserInfo(User user) {
        return 0;
    }
}
